package com.cityp.controller;

import com.cityp.bean.AllUser;
import com.cityp.bean.ResultBean;
import com.cityp.bean.UserLoginParam;
import com.cityp.service.impl.UserListListServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.cityp.service.impl.UserServiceImpl;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.text.ParseException;

/**
 * @ClassName: LoginController
 * @Description:
 * @author: Kowk
 * @since:1.0.0
 */
@RestController
@Api(tags = "登陆以及注册的api")
public class LoginController {

    @Autowired
    private UserServiceImpl userService;


    @ApiOperation(value = "注册账户,参数只需要用户名和密码")
    @PostMapping("/register")
    public ResultBean register(@RequestBody UserLoginParam user){
        return userService.register(user);
    }

    @ApiOperation(value = "登陆之后返回token")
    @PostMapping("/login")
    public ResultBean login(@RequestBody UserLoginParam userLoginParam, HttpServletRequest request) {
        return userService.login(userLoginParam.getUserName(),userLoginParam.getUserPassword(),request);
    }

    @ApiOperation(value = "获取当前用户登陆信息")
    @GetMapping("/admin/info")
    public AllUser getAdminInfo(Principal principal){
        if(principal == null) {
            return null;
        }
        String username = principal.getName();
        AllUser admin = userService.getAdminByUserName(username);
        admin.setUserPassword(null);
        return admin;
    }



    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public ResultBean logout(){
        return ResultBean.success("注销成功");
    }

}
